package com.sec.android.app.myfiles.module.cloud;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.samsung.android.sdk.slinkcloud.CloudGatewayFileBrowserUtils;
import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.MouseKeyboardMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.operation.FileOperator;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

public class CloudFileMenuImp extends AbsMenuImp {

    public CloudFileMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);
        FileRecord record = mFragment.getNavigationInfo().getCurRecord();
        if (!(record instanceof CloudFileRecord)) {
            return;
        }
        setMenuItemVisibility(menu, R.id.menu_create_folder, !(FileOperator.isWorking() || mFragment.getLoadingStatus()));
        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_show_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_hide_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_sync, true);
        setMenuItemEnabled(menu, R.id.menu_sync, !mFragment.getLoadingStatus());
        setMenuItemVisibility(menu, R.id.menu_trash, ((CloudFileRecord) record).getCloudType() == FileRecord.CloudType.SamsungDrive);
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        setMenuItemVisibility(menu, R.id.menu_delete, false);
        setMenuItemVisibility(menu, R.id.menu_move, false);
        setMenuItemVisibility(menu, R.id.menu_copy, false);
        setMenuItemVisibility(menu, R.id.menu_rename, false);
        setMenuItemVisibility(menu, R.id.menu_details, false);

        int selectedCount = mFragment.getSelectedFileCount();

        if (selectedCount > 0) {
            setMenuItemVisibility(menu, R.id.menu_delete, true);
            setMenuItemVisibility(menu, R.id.menu_move, true);
            setMenuItemVisibility(menu, R.id.menu_copy, true);
            setMenuItemVisibility(menu, R.id.menu_details, true);

            if (selectedCount == 1) {
                setMenuItemVisibility(menu, R.id.menu_rename, true);
            }
        }
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        super.createContextualMenuOnBackground(menu);
        menu.removeItem(R.id.menu_share);
        menu.removeItem(R.id.menu_show_hidden_files);
        menu.removeItem(R.id.menu_hide_hidden_files);
    }

    @Override
    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        if (!UiUtils.isSyncing(record)) {
            menu.add(0, R.id.menu_delete, 0, mContext.getResources().getString(R.string.menu_delete));

            if (record.isDirectory() && (MouseKeyboardMgr.isCtrlCopied() || MouseKeyboardMgr.isCtrlMoved())) {
                menu.add(0, R.id.menu_paste, 0, mContext.getResources().getString(R.string.menu_paste));
            } else {
                menu.add(0, R.id.menu_move, 0, mContext.getResources().getString(R.string.menu_move));
                menu.add(0, R.id.menu_copy, 0, mContext.getResources().getString(R.string.menu_copy));
            }
            if (isForOneItem) {
                menu.add(0, R.id.menu_rename, 0, mContext.getResources().getString(R.string.menu_rename));
            }
            menu.add(0, R.id.menu_details, 0, mContext.getResources().getString(R.string.menu_details));
        }
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        int id = item.getItemId();

        if (isNetworkOn(id)) {
            if (item.getItemId() == R.id.menu_open_in_a_new_window) {
                Bundle cloudInfo = new Bundle();
                cloudInfo.putInt(AppConstants.MyFilesExtras.EXTRA_CLOUD_DEVICEID, ((CloudFileRecord) record).getDeviceId());
                cloudInfo.putStringArrayList(AppConstants.MyFilesExtras.EXTRA_CLOUD_PARENTID, ((CloudFileRecord) record).getParentIds());
                cloudInfo.putString(AppConstants.MyFilesExtras.EXTRA_CLOUD_FILEID, ((CloudFileRecord) record).getFileId());
                cloudInfo.putString(AppConstants.MyFilesExtras.EXTRA_CLOUD_FILENAME, record.getName());

                Intent intent = new Intent(AppConstants.Action.ACTION_LAUNCH_MY_FILES);
                intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(AppConstants.MyFilesExtras.EXTRA_START_PATH, record.getFullPath());
                intent.putExtra(AppConstants.MyFilesExtras.EXTRA_START_CLOUD_INFO, cloudInfo);
                mContext.startActivity(intent);
                return true;
            }

            if (fragment != null) {
                srcRecordList = new ArrayList<>();
                srcRecordList.add(record);
                return super._onContextualItemSelected(item, srcRecordList, record, fragment);
            }
        }
        return false;
    }

    @Override
    protected boolean _onOptionsItemSelected(MenuItem item) {
        boolean bRet = true;
        int id = item.getItemId();

        if (isNetworkOn(id)) {
            int processId = mFragment.getProcessId();
            switch (id) {
                case R.id.menu_sync:
                    NavigationInfo info = NavigationManager.getInstance(processId).getCurInfo();
                    if (info != null) {
                        FileRecord curRecord = info.getCurRecord();
                        if (curRecord.getStorageType() == StorageType.Cloud) {
                            CloudFileRecord record = (CloudFileRecord) curRecord;
                            bRet = CloudGatewayFileBrowserUtils.getInstance(mContext).mrrControlCommand(record.getDeviceId(), CloudGatewayFileBrowserUtils.REMOTE_FOLDER_SYNC, null, null, null, null);
                            Toast.makeText(mFragment.context(), getSyncingMsgId(record.getCloudType()), Toast.LENGTH_LONG).show();
                            SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.SYNC, null);
                        } else {
                            Log.e(this, "Current record is not Cloud file!!");
                            bRet = false;
                        }
                    }
                    break;
                case R.id.menu_trash:
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.TRASH, null);
                    MyFilesFacade.goTrash(mFragment.getProcessId());
                    break;
                default:
                    bRet = super._onOptionsItemSelected(item);
            }
        }
        return bRet;
    }

    @Override
    protected boolean showShareMenu() {
        return false;
    }

    private boolean isNetworkOn(int id) {
        boolean canWithNetwork = (id == R.id.menu_sync) || (id == R.id.menu_create_folder) || (id == R.id.menu_delete) || (id == R.id.menu_move) || (id == R.id.menu_copy) || (id == R.id.menu_rename);
        return !canWithNetwork || UiUtils.canExecuteCloud(mActivity, mFragment.getFragmentManager());
    }

    private int getSyncingMsgId(FileRecord.CloudType type) {
        int msgId;

        if (FileRecord.CloudType.GoogleDrive == type) {
            msgId = R.string.syncing_google_drive;
        } else if (FileRecord.CloudType.SamsungDrive == type) {
            if (AppFeatures.IS_JPN) {
                msgId = R.string.syncing_galaxy_drive;
            } else {
                msgId = R.string.syncing_samsung_drive;
            }
        } else {
            msgId = R.string.syncing;
        }

        return msgId;
    }
}
